//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================



using System;
using System.Text.RegularExpressions;

using LitwareHR.Portfolio.Contract;

namespace LitwareHR.Portfolio.Services
{
    public class ProvisioningService : IProvisioningUC
    {
        public void DoProvisioning(ProvisioningRequest request)
        {
            if (String.IsNullOrEmpty(request.TenantName))
                throw new ArgumentNullException("tenantName");

            if (String.IsNullOrEmpty(request.TenantAlias))
                throw new ArgumentNullException("tenantAlias");

            if (String.IsNullOrEmpty(request.AdminPassword))
                throw new ArgumentNullException("adminPassword");

            if (request.DomainModel != 0 && request.DomainModel != 1)
                throw new ArgumentNullException("domainModel");

            Regex r = new Regex(@"\W", RegexOptions.Compiled);
            if (r.IsMatch(request.TenantAlias))
                throw (new ArgumentException("tenantAlias must not contain non-word characters", "request"));

            if (!String.IsNullOrEmpty(request.SilverLightAccountId) && !String.IsNullOrEmpty(request.SilverLightAccountKey))
            {
                ProvisioningLogic.DoProvisioning(request.TenantName, request.TenantAlias, request.DomainModel, request.AdminPassword, request.SilverLightAccountId, request.SilverLightAccountKey);
            }
            else
            {
                ProvisioningLogic.DoProvisioning(request.TenantName, request.TenantAlias, request.DomainModel, request.AdminPassword);
            }
        }
    }
}
